package id3.utils;

import id3.models.RowData;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tungck
 */
public class LoadFile {
    public static final int NUMBER_ATTRIBUTE = 16;
    public static ArrayList<RowData> getData(String fileName) {
        ArrayList<RowData> data =loadFileAndCreateData(fileName);
        return data;
    }
    
    
    private static ArrayList<RowData> loadFileAndCreateData(String fileName) {
        ArrayList<RowData> listRow = new ArrayList<>();
        BufferedReader bufferReader ;
        try {
            bufferReader= new BufferedReader(new FileReader(fileName));
            String line = bufferReader.readLine();           
            while(line != null){
                ArrayList<String> listAttrs;
                String[] value = line.split(",");
                listAttrs = new ArrayList<>(Arrays.asList(value)) ;
                if (NUMBER_ATTRIBUTE != value.length) {
                    throw new Exception("Error: Number of attributes must be: " + NUMBER_ATTRIBUTE);
                }
                line = bufferReader.readLine();
                listRow.add(new RowData(listAttrs));
            }
            bufferReader.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(LoadFile.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("flie not found");
        } catch (IOException ex) {
            Logger.getLogger(LoadFile.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("IO exception not found");
        } catch (Exception ex) {
            Logger.getLogger(LoadFile.class.getName()).log(Level.SEVERE, null, ex);
            System.out.println("exception flie not found: "+ex.getMessage());
        }
        
        return listRow;
    }
    
//    public static ArrayList<RowData> loadFileTest(String fileTest) {
//        ArrayList<RowData> listRow = new ArrayList<>();
//        try {
//            BufferedReader bufferReader = new BufferedReader(new FileReader(fileTest));
//            String line = bufferReader.readLine();
//            
//            while(line != null){
//                ArrayList<String> listAttrs = new ArrayList<>();
//                String[] value = line.split(",");
//                listAttrs = new ArrayList<>(Arrays.asList(value)) ;
//                if ((NUMBER_ATTRIBUTE) != value.length) {
//                    throw new Exception("Error: Number of attributes must be: " + (NUMBER_ATTRIBUTE-1));
//                }
////                for (int i = 0; i < NUMBER_ATTRIBUTE-1; i++) {
////                    String[] arr = Constants.getMapAttrs()[i].getStringValues();
////                    if (!Arrays.asList(arr).contains(value[i])) {
////                        System.out.println(" Attribute A"+i + "not value " + value[i]);
////                        return null;
////                    }
////                    
////                }
//                line = bufferReader.readLine();
//                listRow.add(new RowData(listAttrs));
//                
//            }
//            
//        } catch (FileNotFoundException ex) {
//            Logger.getLogger(LoadFile.class.getName()).log(Level.SEVERE, null, ex);
//            System.out.println("flie not found");
//        } catch (IOException ex) {
//            Logger.getLogger(LoadFile.class.getName()).log(Level.SEVERE, null, ex);
//            System.out.println("IO exception not found");
//        } catch (Exception ex) {
//            Logger.getLogger(LoadFile.class.getName()).log(Level.SEVERE, null, ex);
//            System.out.println("exception flie not found: "+ex.getMessage());
//        }
//        return listRow;
//    }

}
